package com.jackiechancinema.ssm.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.DrillBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.DreamDao;
import com.jackiechancinema.ssm.dao.DrillDao;
import com.jackiechancinema.ssm.entity.Dreams;
import com.jackiechancinema.ssm.entity.Drills;
import com.jackiechancinema.ssm.service.Inte.DrillService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DrillServiceImpl implements DrillService {

    @Autowired
    private DrillDao drillDao;

    @Override
    public PageVo<Drills> findAllV2(DrillBo bo) {
        if (bo != null){
            //组装查询条件
            Drills drills = new Drills();
            if (StringUtils.hasLength(bo.getSite())){
                drills.setSite(bo.getSite());
            }
            if (StringUtils.hasLength(bo.getTime())){
                drills.setTime(bo.getTime());
            }

            PageHelper.startPage(bo.getPage(),bo.getLimit());
            PageInfo<Drills> pageInfo = new PageInfo<>(drillDao.queryAllV2(drills));
            return new PageVo<Drills>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());

        }
        return new PageVo<Drills>(SystemContant.PAGE_R,"OK",0 ,null);
    }

    @Override
    public PageInfo<Drills> findAll(int page, int limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Drills> pageInfo = new PageInfo<Drills>(drillDao.queryAll());
        return pageInfo;
    }

    @Override
    public R DrillAdd(Drills drills) {
        int i = drillDao.insertAdd(drills);
        System.out.println(i);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R DrillUpdate(Drills drills) {
        int i = drillDao.updateUpdate(drills);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R DrillDel(int id) {
        int i = drillDao.deleteDel(id);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }
}
